import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Cubie{
	CubieColor cubie;
	float imageHeight, imageWidth;
	int xCoordinate, yCoordinate;
	int color, red, green, blue;
	public static BufferedImage img = null;
	
	public Cubie(double xScaling, double yScaling){
		
		try{
			img = ImageIO.read(new File("C:/users/joey/desktop/sides/cube2.jpg"));
		}catch(IOException e){
			System.out.println("Catastrophic error, file couldn't be loaded.");
		}
		
		imageHeight = img.getHeight();
		imageWidth = img.getWidth();
		
		this.xCoordinate = (int) (xScaling * imageWidth);
		this.yCoordinate = (int) (yScaling * imageHeight);
		
		color = img.getRGB(this.xCoordinate, this.yCoordinate);
		red = (color >> 16) & 0xFF;
		green = (color >> 8) & 0xFF;
		blue = (color ) & 0xFF;
		System.out.println(red);
		System.out.println(green);
		System.out.println(blue);
		String hexValue = rgbToHex(red, green, blue);
		int decValue = hexToDecimal(hexValue);
		cubie = returnColor(decValue);
	}
	
	public CubieColor returnColor(int decValue){
		int red = hexToDecimal("D40017");
		int orange = hexToDecimal("CF310A");
		int yellow = hexToDecimal("F7FF00");
		int green = hexToDecimal("22FF00");
		int blue = hexToDecimal("0000FF");
		int white = hexToDecimal("DFC9B8");
		int [] decValues = {red, orange, yellow, green, blue, white};
		int indexSmallest = 0;
		int smallest = Integer.MAX_VALUE;
		int [] differences = new int[6];
		differences[0] = Math.abs(red - decValue);
		differences[1] = Math.abs(orange - decValue);
		differences[2] = Math.abs(yellow - decValue);
		differences[3] = Math.abs(green - decValue);
		differences[4] = Math.abs(blue - decValue);
		differences[5] = Math.abs(white - decValue);
		for(int i = 0; i <= 5; i++){
			if(differences[i] < smallest){
				smallest = differences[i];
				indexSmallest = i;
			}
		}
		float percentAccuracyMainColor = (decValue / (decValues[indexSmallest]));
		System.out.println("Boom: " + percentAccuracyMainColor);
		return CubieColor.values()[indexSmallest];
	}
	
	public String rgbToHex(int red, int green, int blue){
		String hexValue = "";
		String newRed = Integer.toHexString(red);
		String newGreen = Integer.toHexString(green);
		String newBlue = Integer.toHexString(blue);
		if(red < 16){
			hexValue = hexValue.concat("0");
		}
		hexValue = hexValue.concat(newRed);
		if(green < 16){
			hexValue = hexValue.concat("0");
		}
		hexValue = hexValue.concat(newGreen);
		if(blue < 16){
			hexValue = hexValue.concat("0");
		}
		hexValue = hexValue.concat(newBlue);
		hexValue = hexValue.toUpperCase();
		System.out.println("Hex: " + hexValue);
		return hexValue;
	}
	
	int hexToDecimal(String hexValue){
		int decValue = 0;
		int currentChar;
		int [] characters = new int[6];
		double [] powers = {Math.pow(16, 5), Math.pow(16, 4), Math.pow(16, 3), Math.pow(16, 2), 16, 1};
		for(int i = 0; i < 6; i++){
			currentChar = hexValue.charAt(i);
			if((hexValue.codePointAt(i) >= 65 && hexValue.codePointAt(i) <= 70) || (hexValue.codePointAt(i) >= 97 && hexValue.codePointAt(i) <= 102)){
				currentChar = Character.toUpperCase(currentChar);
				switch(currentChar){
				case 'A':
					characters[i] = 10;
					break;
				case 'B':
					characters[i] = 11;
					break;
				case 'C':
					characters[i] = 12;
					break;
				case 'D':
					characters[i] = 13;
					break;
				case 'E':
					characters[i] = 14;
					break;
				case 'F':
					characters[i] = 15;
					break;
				}
			}else{
				characters[i] = Character.getNumericValue(hexValue.charAt(i));
			}
			
			decValue += (characters[i] * powers[i]);
			
		}
		System.out.println("Dec: " + decValue);
		return decValue;
	}
}